﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mitasoft.AdsShedule.Common;

namespace mitasoft.AdsShedule.Forms
{
    public partial class VoiceSelection : Form
    {
        public AppoimentButtonInfo ButtonInfo { get; set; }

        public List<Business.VoiceBO> VoiceData { get; private set; }
        public List<Business.VoiceBO> SelectedVoices { get; private set; }

        public VoiceSelection()
        {
            InitializeComponent();
            this.Load += new EventHandler(VoiceSelection_Load);
        }

        void VoiceSelection_Load(object sender, EventArgs e)
        {
            VoiceData = Business.BusinessAccess.Instance.GetVoiceList();
            SelectedVoices = new List<Business.VoiceBO>();

            this.datagridVoice.AutoGenerateColumns = false;
            this.datagridVoice.DataSource = VoiceData;

            this.CancelButton = buttonClose;
            this.dataGridSelectedVoices.AutoGenerateColumns = false;

            lblData.Text = string.Format("{0} - {1}", ButtonInfo.Data.ToString("MMM-yyyy"), ButtonInfo.InvervalType.ToString());

            PopulateAllReadySelectedAssociations();
        }

        private void PopulateAllReadySelectedAssociations()
        {
            //TODO: Data consistency
            var queryVoices = VoiceData.Where(item => ButtonInfo.VoiceAssocID.Contains(item.IDVoiceAssoc)).ToList();

            dataGridSelectedVoices.DataSource = queryVoices;
            SelectedVoices = queryVoices;
        }

        private void CloseWindow()
        {
            ButtonInfo.VoiceAssocID = SelectedVoices.Select(item => item.IDVoiceAssoc).ToList();

            this.DialogResult = System.Windows.Forms.DialogResult.No;
            this.Close();
        }
        private void SelectVoice()
        {
            if (this.datagridVoice.CurrentRow != null)
            {
                Business.VoiceBO selectedVoice = (Business.VoiceBO)this.datagridVoice.CurrentRow.DataBoundItem;
                ButtonInfo.VoiceAssocID = SelectedVoices.Select(item => item.IDVoiceAssoc).ToList();

                this.DialogResult = System.Windows.Forms.DialogResult.Yes;
                this.Close();
            }
            else
            {
                MessageBox.Show("Please select a voice.");
            }
        }

        private void buttonSelectVoice_Click(object sender, EventArgs e)
        {
            SelectVoice();
        }
        private void buttonClose_Click(object sender, EventArgs e)
        {
            CloseWindow();
        }
        private void tbSearchClient_TextChanged(object sender, EventArgs e)
        {
            var tb = (TextBox)sender;
            List<Business.VoiceBO> filteredData = VoiceData.Where(item => item.ClientName.ToLower().Contains(tb.Text.ToLower())).ToList();

            this.datagridVoice.DataSource = filteredData;
        }
        private void datagridVoice_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.AddToSelectionList();
        }



        //
        //Voice Selection Part
        //

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddToSelectionList();
        }
        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (dataGridSelectedVoices.CurrentRow != null)
            {
                Business.VoiceBO selectedVoice = (Business.VoiceBO)this.dataGridSelectedVoices.CurrentRow.DataBoundItem;
                SelectedVoices.Remove(selectedVoice);
                BindToSelectedVoicesGrid();
            }
        }

        private void AddToSelectionList()
        {
            Business.VoiceBO selectedVoice = (Business.VoiceBO)this.datagridVoice.CurrentRow.DataBoundItem;
            SelectedVoices.Add(selectedVoice);

            BindToSelectedVoicesGrid();
        }
        private void BindToSelectedVoicesGrid()
        {
            dataGridSelectedVoices.DataSource = null;
            dataGridSelectedVoices.DataSource = SelectedVoices;
            dataGridSelectedVoices.Refresh();
        }
    }
}
